from datetime import datetime

from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_stock.entity.vo.transfer_vo import DeleteTransferModel, TransferModel, TransferPageQueryModel
from module_stock.service.transfer_service import TransferService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

transferController = APIRouter(prefix='/stock/transfer', dependencies=[Depends(LoginService.get_current_user)])


@transferController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:transfer:list'))]
)
async def get_stock_transfer_list(
        request: Request,
        transfer_page_query: TransferPageQueryModel = Depends(TransferPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    transfer_page_query_result = await TransferService.get_transfer_list_services(query_db, transfer_page_query,
                                                                                  is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=transfer_page_query_result)


@transferController.post('', dependencies=[Depends(CheckUserInterfaceAuth('stock:transfer:add'))])
@ValidateFields(validate_model='add_transfer')
@Log(title='银证转账', business_type=BusinessType.INSERT)
async def add_stock_transfer(
        request: Request,
        add_transfer: TransferModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_transfer.create_time = datetime.now()
    add_transfer.update_time = datetime.now()
    add_transfer_result = await TransferService.add_transfer_services(query_db, add_transfer)
    logger.info(add_transfer_result.message)

    return ResponseUtil.success(msg=add_transfer_result.message)


@transferController.put('', dependencies=[Depends(CheckUserInterfaceAuth('stock:transfer:edit'))])
@ValidateFields(validate_model='edit_transfer')
@Log(title='银证转账', business_type=BusinessType.UPDATE)
async def edit_stock_transfer(
        request: Request,
        edit_transfer: TransferModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_transfer.update_time = datetime.now()
    edit_transfer_result = await TransferService.edit_transfer_services(query_db, edit_transfer)
    logger.info(edit_transfer_result.message)

    return ResponseUtil.success(msg=edit_transfer_result.message)


@transferController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('stock:transfer:remove'))])
@Log(title='银证转账', business_type=BusinessType.DELETE)
async def delete_stock_transfer(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_transfer = DeleteTransferModel(ids=ids)
    delete_transfer_result = await TransferService.delete_transfer_services(query_db, delete_transfer)
    logger.info(delete_transfer_result.message)

    return ResponseUtil.success(msg=delete_transfer_result.message)


@transferController.get(
    '/{id}', response_model=TransferModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:transfer:query'))]
)
async def query_detail_stock_transfer(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    transfer_detail_result = await TransferService.transfer_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=transfer_detail_result)


@transferController.post('/amount_by_tag')
async def get_transfer_amount_by_account_tag(
        request: Request,
        query_db: AsyncSession = Depends(get_db)
):
    """
    根据账号标签统计转账金额
    """
    result = await TransferService.get_transfer_amount_by_account_tag_services(query_db)
    logger.info('按账号标签统计转账金额成功')
    return ResponseUtil.success(data=result)
